package com.migraine.service.impl;

import com.migraine.model.Users;
import com.migraine.service.UsersService;
import com.migraine.mapper.UsersMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



/**
 * 用户service,用户名唯一
 */
@Service
public class UsersServiceImpl implements UsersService {

    @Autowired
    private UsersMapper usersMapper;

    // 根据用户名得到用户
    @Override
    public Users getUsers(String username) {
        //没根据名字查询到用户就返回Null
        return usersMapper.getUsersByName(username);

    }

    // 往数据库中增加用户
    @Override
    public boolean insertUsers(String username, String password) {
        Users user = usersMapper.getUsersByName(username);
        // 如果存在同名的
        if (user != null) return false;
        else {
            usersMapper.insertUsers(username, password);
            return true;
        }
    }

    // 将名字为name1的用户修改用户名为name2
    @Override
    public boolean updateNameByName(String name1, String name2) {
        Users user = usersMapper.getUsersByName(name2);
        // 如果有与name2同名的用户 则不能修改成功
        if (user != null) return false;
        else {
            usersMapper.updateNameByName(name1, name2);
            return true;
        }
    }

    // 将名字为name用户的密码改为pwd
    @Override
    public boolean updatePwdByName(String name, String pwd) {
        Users user = usersMapper.getUsersByName(name);
        if (user != null) {
            usersMapper.updatePwdByName(name, pwd);
            return true;
        }
        return false;
    }

    // 修改用户头像
    @Override
    public boolean updateImgById(int id, String img) {

        return usersMapper.updateImageById(id, img);

    }

    // 删除用户
    @Override
    public boolean deleteByName(String username) {
        Users users = usersMapper.getUsersByName(username);
        if (users != null) {
            usersMapper.deleteByName(username);
            return true;
        }
        return false;
    }


    @Override
    public boolean deleteUsers(String username) {
        return false;
    }

    @Override
    public boolean changeName(String username) {
        return false;
    }

    @Override
    public boolean changePwd(String username) {
        return false;
    }

    @Override
    public Users getUsers(int id) {
        return usersMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean setPhoto(String name,String photo) {
        return usersMapper.setPhoto(name,photo);
    }
}
